package cn.life.common.push;

import cn.life.common.entity.vo.PushVo;
import cn.life.common.push.hw.HwPush;
import cn.life.common.push.ios.IosPush;
import cn.life.common.push.jg.JgPush;
import net.sf.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description:    推送服务
 *
 * @Author 李佳明
 * @Date 2018-09-06 13:10
 */
public class PushService {

    /**
     * 推送
     */
    public void push(PushVo pushVo) throws Exception {
        switch (pushVo.getState()){
            case 2 :
                new IosPush().iosPush(pushVo);
                break;
            case 11 :
                new JgPush().androidPush(pushVo);
                break;
            case 12 :
                new HwPush().androidPush(pushVo);
                break;
        }
    }

    public PushVo pushVoInit(String title,String content,int state,String pushMark){
        PushVo pushVo = new PushVo();
        pushVo.setTitle(title);
        pushVo.setContent(content);
        pushVo.setState(state);
        pushVo.setMessageType(0);
        pushVo.setCount(1);
        pushVo.setExtra(new JSONObject());

        List<String> userList = new ArrayList<String>();
        userList.add(pushMark);
        pushVo.setUserList(userList);

        return pushVo;
    }

    public PushVo pushVoInit(String title,String content,String type,List<String> pushMarkList){
        PushVo pushVo = new PushVo();
        pushVo.setTitle(title);
        pushVo.setContent(content);
        switch (type){
            case "jg" :  pushVo.setState(11); break;
            case "hw" :  pushVo.setState(12); break;
            case "ios" :  pushVo.setState(2); break;
        }
        pushVo.setMessageType(0);
        pushVo.setCount(1);
        pushVo.setExtra(new JSONObject());

        pushVo.setUserList(pushMarkList);

        return pushVo;
    }

    public void allPush(List<Map<String,Object>> mapList) throws Exception {
        List<String> jgMarkList = new ArrayList<>();
        List<String> hwMarkList = new ArrayList<>();
        List<String> iosMarkList = new ArrayList<>();
        for(Map<String,Object> map : mapList){
            if(map.get("jgPushMark") != null && !map.get("jgPushMark").equals("")){
                jgMarkList.add(map.get("jgPushMark").toString());
            }else if(map.get("hwPushMark") != null && !map.get("hwPushMark").equals("")){
                hwMarkList.add(map.get("hwPushMark").toString());
            }else{
                iosMarkList.add(map.get("iosPushMark").toString());
            }
        }

        if(jgMarkList.size() > 0){
            PushVo jgpushVo = pushVoInit("提料单","您有新的提料单需要审查，请查收","jg",jgMarkList);
            push(jgpushVo);
        }

        if(hwMarkList.size() > 0){
            PushVo hwpushVo = pushVoInit("提料单","您有新的提料单需要审查，请查收","hw",hwMarkList);
            push(hwpushVo);
        }

        if(iosMarkList.size() > 0){
            PushVo iospushVo = pushVoInit("提料单","您有新的提料单需要审查，请查收","ios",iosMarkList);
            push(iospushVo);
        }
    }


/*
    public static void  main(String[] args) throws IOException {
        PushVo pushVo = new PushVo();
        pushVo.setTitle("标题");
        pushVo.setCount(1);
        pushVo.setExtra(null);
        pushVo.setMessageType(0);
        pushVo.setState(1);
        pushVo.setContent("内容");

        String userId = "0869979035363188300002359700CN01";
        List<String> list = new ArrayList<>();
        list.add(userId);

        pushVo.setUserList(list);
        try {
            new HwPush().androidPush(pushVo);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }*/
}
